let token = sessionStorage.getItem("token");
$(function () {

    let name = sessionStorage.getItem("username");
    let role = sessionStorage.getItem("role");
    $("#name").append(name);
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
    //接收URL中的参数id
    var id = getUrlParam('id');
    $.ajax({
        url: testurl + "motion/detail",
        type: "POST",
        headers: { "Authorization": "Bearer " + token},
        contentType: "application/json",
        data: JSON.stringify({
            "id": id
        }),
        success: function (data) {
            let check_flag = data.obj.check_flag;
            let status = data.obj.status;

            //委员

            if (status !== "9"){
                $("#dcssh").css("display","none");
            }

            if (role === "db") {
                //不显示回复
                $("#writerdform").css("display","none");
                $("#adddcszd").css("display","none");
                $("#adddf").css("display","none");
                $("#adddcssh").css("display","none");
                if (status < "6"){
                    $("#adddanwei").css("display","none");
                    $("#bumen").css("display","none");
                    $("#dcs").css("display","none");
                    $("#dcssh").css("display","none");
                }else if (status === "6") {
                    $("#adddanwei").css("display",null);
                    $("#bumen").css("display",null);
                    $("#dcs").css("display",null);
                    $("#dcssh").css("display",null);
                    $("#pjnr").css("display","none")
                }else if (status === "9"){
                    $("#adddanwei").css("display",null);
                    $("#bumen").css("display",null);
                    $("#dcs").css("display",null);
                    $("#dcssh").css("display",null);
                    $("#addpj").css("display","none")
                }
            }
            //人代室
            if (role === "rds"){
                $("#adddcszd").css("display","none");
                $("#adddf").css("display","none");
                $("#adddcssh").css("display","none");
                $("#addpj").css("display","none");
                if (status !== "1") {
                    $("#writerdform").css("display","none");
                }else {
                    $("#readrdform").css("display","none");
                }

                if(status < "6"){
                    $("#adddanwei").css("display","none");
                    $("#bumen").css("display","none");
                    $("#dcs").css("display","none");
                    $("#dcssh").css("display","none");
                }
            }
            //督查室
            if (role === "dcs"){
                $("#addpj").css("display","none");
                $("#writerdform").css("display","none");
                $("#adddf").css("display","none");
                if (status === "2"){
                    $("#checkdb").css("display","none");
                    $("#bumen").css("display","none");
                    $("#dcs").css("display","none");
                }else if (status === "3") {
                    $("#bumen").css("display","none");
                    $("#dcs").css("display","none");
                    $("#adddcszd").css("display","none");
                }else if (status === "4") {
                    $("#addsh").css("display","none");
                    $("#adddcszd").css("display","none");
                } else if (status === "6") {
                    $("#adddcssh").css("display","none");
                    $("#adddcszd").css("display","none");
                }else if (status === "9") {
                    $("#adddcssh").css("display","none");
                    $("#adddcszd").css("display","none");
                }
            }
            //政府部门
            if (role === "zf"){
                $("#adddcszd").css("display","none");
                $("#adddcssh").css("display","none");
                $("#addpj").css("display","none");

                $("#writerdform").css("display","none");
                if (status ==="3"){
                    $("#checkdf").css("display","none");
                    $("#dcs").css("display","none");
                }else if (status ==="3" && check_flag ==="0"){
                    $("#dcs").css("display",null);
                    $("#adddcssh").css("display","none");
                }else  if(status  === "4"){
                    $("#adddf").css("display","none");
                    $("#dcs").css("display","none");
                }else  if(status  === "6" || status === "9"){
                    $("#adddf").css("display","none");
                }
            }










            let url = testurl + "";
            // $(".shenpistate").html(data.obj.status_name);
            $("#title").val(data.obj.title);
            $("#group_name").val(data.obj.group_name);
            if (data.obj.nature === "1"){
                $(".type").eq(0).attr("checked","")
            } else if (data.obj.nature === "2"){
                $(".type").eq(1).attr("checked","")
            }else if(data.obj.nature === "3"){
                $(".type").eq(2).attr("checked","")
            }else if(data.obj.nature === "4"){
                $(".type").eq(3).attr("checked","")
            }
            $("#commit_date").val(data.obj.commit_date);
            $("#link_addr").val(data.obj.link_addr);
            $("#leader_name").val(data.obj.leader_name);
            $("#leader_tel").val(data.obj.leader_tel);
            $("#jointlyers_name").val(data.obj.jointlyers_name);
            $("#content").val(data.obj.content);
            $("#zip_code").val(data.obj.zip_code);
            $("#rdopinion").val(data.obj.rds_remark);

            //查看督查室提交过来的
            $("#main_handler_organ").html(data.obj.main_handler_organ);
            $("#main_handler_dwxt").html(data.obj.main_handler_dwxt);
            $("#main_handler_name").html(data.obj.main_handler_name);
            $("#main_handler_phone").html(data.obj.main_handler_phone);
            $("#appoint_remark").val(data.obj.appoint_remark);
            $("#latest_time1").val(data.obj.latest_time);
            $("#check_remark").val(data.obj.check_remark);
            //部门提交过来的
            $("#recordcontent").val(data.obj.reply);
            $("#dbassess").val(data.obj.assess);
            let str4 = "";
            $.each(data.obj.reply_pic_list,function (index,value) {
                str4 +="<li style='max-width: 150px'>\n" +
                    "        <div class=\"am-gallery-item\">\n" +
                    "            <a href=\""+url+""+value.path+"\" class=\"\">\n" +
                    "              <img src=\""+url+""+value.path+"\"/>\n" +
                    "            </a>\n" +
                    "        </div>\n" +
                    "      </li>";
            });
            let str5 = "";
            $.each(data.obj.reply_video_list,function (index,value) {
                str5 += "<li class='am-fl'><video src=\""+url+""+value.path+"\" controls=\"controls\"></video></li>";
            });

            let str6 = "";
            $.each(data.obj.reply_audio_list,function (index,value) {
                str6 += "<li class='am-fl'><audio src=\""+url+""+value.path+"\" controls=\"controls\"></audio></li>";
            });

            let str7 = "";
            $.each(data.obj.reply_file_list,function (index,value) {
                str7 += " <a download href=\""+url+""+value.path+"\">"+value.name+"</a>";
            });
            let str3 = "";
            $.each(data.obj.join_handlers,function (index,res) {
                str3 +="<div class=\"am-u-sm-3 am-u-end\">\n" +
                    "                        <div class=\"addhuiban\">\n" +
                    "                            <div class=\"am-u-sm-12\">\n" +
                    "                            <label class=\"am-u-sm-6 am-form-label zcolor\">会办单位：</label>\n" +
                    "                            <div class=\"am-u-sm-6\">\n" +
                    "                                <label class=\" am-form-label \">"+res.organ_name+"</label>\n" +
                    "                            </div>\n" +
                    "                            </div>\n" +
                    "                            <div class=\"am-u-sm-12\">\n" +
                    "                            <label class=\"am-u-sm-6 am-form-label zcolor\">单位系统：</label>\n" +
                    "                            <div class=\"am-u-sm-6\">\n" +
                    "                                <label class=\" am-form-label \">"+res.dwxt+"</label>\n" +
                    "                            </div>\n" +
                    "                            </div>\n" +
                    "                            <div class=\"am-u-sm-12\">\n" +
                    "                            <label class=\"am-u-sm-6 am-form-label zcolor\">联系人：</label>\n" +
                    "                            <div class=\"am-u-sm-6\">\n" +
                    "                                <label class=\" am-form-label \">"+res.realname+"</label>\n" +
                    "                            </div>\n" +
                    "                            </div>\n" +
                    "                            <div class=\"am-u-sm-12\" style=\"padding-bottom: 10px\">\n" +
                    "                            <label class=\"am-u-sm-6 am-form-label zcolor\">联系电话：</label>\n" +
                    "                            <div class=\"am-u-sm-6\">\n" +
                    "                                <label class=\" am-form-label \">"+res.phone+"</label>\n" +
                    "                            </div>\n" +
                    "                            </div>\n" +

                    "                        </div>\n" +
                    "                    </div>"
            });
            let imgsrc = data.obj.new_pic_list;
            let str = "";

            $.each(imgsrc, function (index, value) {

                str += "<li>\n" +
                    "        <div class=\"am-gallery-item\">\n" +
                    "            <a href=\""+url+""+value.path+"\" class=\"\">\n" +
                    "              <img src=\""+url+""+value.path+"\"/>\n" +
                    "            </a>\n" +
                    "        </div>\n" +
                    "      </li>";
            });
            let videosrc = data.obj.new_video_list;
            let str2 = "";
            $.each(videosrc,function (index,values) {

                str2 += "<li class='am-fl'><video src=\""+url+""+values.path+"\" controls=\"controls\"></video></li>";
            });
            $("#show ul").eq(0).html(str);
            $("#show ul").eq(1).html(str2);
            $("#huiban2").find("div").eq(0).after(str3);
            $("#show2 ul").eq(0).html(str4);
            $("#show2 ul").eq(1).html(str5);
            $("#show2 ul").eq(2).html(str6);
            $("#show2 ul").eq(3).html(str7);
        }
    });

    //人代室回复
    $(".rdrecord").click(function () {
        let status = $(this).attr("id");
        let rds_remark = $("#recordopinion").val();
        $.ajax({
            url: testurl + "motion/rds_commit",
            type: "POST",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "id":id,
                "rds_remark":rds_remark,
                "status":status//0未通过 2通过
            }),
            success:function (data) {
                if (data.success === true) {
                    $(".users").html("审批意见");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }else {
                    $(".users").html("审批意见");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }
            }
        })
    });
    //督查室审核
    $(".dcsrecord").click(function () {
        let status2 = $(this).attr("id");
        let check_remark = $("#dcssuggest").val();
        $.ajax({
            url: testurl + "motion/check",
            type: "POST",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "id":id,
                "check_remark":check_remark,
                "status":status2//0未通过 2通过
            }),
            success:function (data) {
                if (data.success === true) {
                    $(".users").html("审批意见");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }else {
                    $(".users").html("审批意见");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }
            }
        })
    });
    //委员评价
    $("#assess").click(function () {
        let assess = $("#assesscontent").val();
        $.ajax({
            url: testurl + "motion/assess",
            type: "POST",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "id":id,
                "assess":assess
            }),
            success:function (data) {
                if (data.success === true) {
                    $(".users").html("委员评价");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }else {
                    $(".users").html("委员评价");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }
            }
        })

    });
    //添加主办代办单位
    let userList = "";

        $.ajax({
            url: testurl + "users/list_all",
            type: "POST",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "zbdw":"1"//主办单位
            }),
            success: function (data) {

                $.each(data.obj,function (index,value) {
                    userList += "<span value=\""+value.id+"\" onclick=\"changeColor(this)\">"+value.realname+"</span>&nbsp;&nbsp;";

                });
                $("#userList").html(userList);
                $("#userList1").html(userList);
            }
        });


//督查室提交指定的内容
    $("#zhiding").click(function () {
       let appoint_remark =  $("#beizhu").val();
       let latest_time = $("#latest_time").val();
       let aa = join_handler.toString();
        $.ajax({
            url: testurl + "motion/appoint",
            type: "POST",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "id":id,
                "main_handler":dad,
                "join_handler":aa,//用户id串
                "appoint_remark":appoint_remark,
                "latest_time":latest_time,//最晚办理时间
            }),
            success:function (data) {
                if (data.success === true) {
                    $(".users").html("提交主办会办信息");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }else {
                    $(".users").html("提交主办会办信息");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }
            }
        });
        return false
    });


//部门返回给督查室
    // 图片列表
    let reply_pic_list = [];
    // 视频列表
    let reply_video_list = [];
    // 视频列表
    let reply_audio_list = [];
    // 视频列表
    let reply_file_list = [];
    //视频名称
    $('#form-video').on('change', function() {
        let fileNames = '';
        $.each(this.files, function() {
            fileNames += '<span class="am-badge">' + this.name + '</span> ';
        });
        $('#video-list').html(fileNames);

    });
    //音频名称
    $('#form-audio').on('change', function() {
        let fileNames = '';
        $.each(this.files, function() {
            fileNames += '<span class="am-badge">' + this.name + '</span> ';
        });
        $('#audio-list').html(fileNames);

    });
    //文件名称
    $('#form-file').on('change', function() {
        let fileNames = '';
        $.each(this.files, function() {
            fileNames += '<span class="am-badge">' + this.name + '</span> ';
        });
        $('#file-list').html(fileNames);

    });
    $("#record").click(function () {


        // 图片上传
        let formData = new FormData();
        let pic = document.getElementById("file").files;

        for(let j=0;j<pic.length;j++){
            formData.append(""+j+"",pic[j]);
        }
        $.ajax({
            url : testurl + "motion/file",
            type: 'post',
            headers: { "Authorization": "Bearer " + token},
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success : function(data) {
                reply_pic_list = data.obj;
                console.log(data);
            },
            error : function(data) {
                console.log(data);
            }
        });
        //视频上传

        formData = new FormData();
        let pic2 = document.getElementById("form-video").files;

        for(let n=0;n<pic2.length;n++){
            formData.append(""+n+"",pic2[n]);
        }
        $.ajax({
            url : testurl + "motion/file",
            type: 'post',
            headers: { "Authorization": "Bearer " + token},
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success : function(data) {
                reply_video_list = data.obj;
                console.log(data);
            },
            error : function(data) {
                console.log(data);
            }
        });
        // 音频上传
        formData = new FormData();
        let pic3 = document.getElementById("form-audio").files;

        for(let q=0;q<pic3.length;q++){
            formData.append(""+q+"",pic3[q]);
        }
        $.ajax({
            url : testurl + "motion/file",
            type: 'post',
            headers: { "Authorization": "Bearer " + token},
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success : function(data) {
                reply_audio_list = data.obj;
                console.log(data);
            },
            error : function(data) {
                console.log(data);
            }
        });
        // 文件上传
        formData = new FormData();
        let pic4 = document.getElementById("form-file").files;

        for(let w=0;w<pic4.length;w++){
            formData.append(""+w+"",pic4[w]);
        }
        $.ajax({
            url : testurl + "motion/file",
            type: 'post',
            headers: { "Authorization": "Bearer " + token},
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success : function(data) {
                reply_file_list = data.obj;
                console.log(data);
            },
            error : function(data) {
                console.log(data);
            }
        });
        let reply = $("#reply").val();
        let reply_list = {"id":id,"reply":reply,"reply_pic_list":reply_pic_list,"reply_video_list":reply_video_list,
            "reply_audio_list":reply_audio_list,"reply_file_list":reply_file_list};
        $.ajax({
            async:false,
            url: testurl + "motion/reply",
            type: "POST",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify(reply_list),
            success:function (data) {
                if (data.success === true) {
                    $(".users").html("承办答复");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }else {
                    $(".users").html("承办答复");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    $('#leave').modal('close');
                    $("#return").click(function () {
                        window.location.href = "yianmessage.html";
                    })
                }
            }
        });
        return false
    });


    //督查室批复部门

});
function changeColor(e) {
    $(e).addClass("spanActive");
    $(e).siblings("span").removeClass("spanActive");
}
// 主办
let dad = "";
$("#go3").click(function () {
    if($("#userList span").hasClass("spanActive")){
        dad = $(".spanActive").attr("value");

        $.ajax({
            url: testurl + "motion/handler_info",
            type: "POST",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "user_id":dad//主办单位
            }),
            success:function (data) {

                $("#zhuban").html(data.obj.organ_name);
                $("#danwei").html(data.obj.dwxt);
                $("#contact").html(data.obj.realname);
                $("#phone").html(data.obj.phone);
            }
        });
        $('#add').modal('close');
        $("#userList span").removeClass("spanActive");
    }
    else {
        alert("请选择部门！")
    }

});
let bbb = "11";
$("#addhuiban").click(function () {
    let box = '<div class="am-u-sm-3 am-u-end" id="'+bbb+'">\n' +
        '                        <div class="addhuiban" >\n' +
        '                            <div class="am-u-sm-12">\n' +
        '                            <label  class="am-u-sm-6 am-form-label zcolor">会办单位：</label>\n' +
        '                            <div class="am-u-sm-6">\n' +
        '                                <label  class=" am-form-label zhuban"></label>\n' +
        '                            </div>\n' +
        '                            </div>\n' +
        '                            <div class="am-u-sm-12">\n' +
        '                            <label  class="am-u-sm-6 am-form-label zcolor">单位系统：</label>\n' +
        '                            <div class="am-u-sm-6">\n' +
        '                                <label  class=" am-form-label danwei"></label>\n' +
        '                            </div>\n' +
        '                            </div>\n' +
        '                            <div class="am-u-sm-12">\n' +
        '                            <label  class="am-u-sm-6 am-form-label zcolor">联系人：</label>\n' +
        '                            <div class="am-u-sm-6">\n' +
        '                                <label  class=" am-form-label contact" ></label>\n' +
        '                            </div>\n' +
        '                            </div>\n' +
        '                            <div class="am-u-sm-12" style="padding-bottom: 10px">\n' +
        '                            <label  class="am-u-sm-6 am-form-label zcolor">联系电话：</label>\n' +
        '                            <div class="am-u-sm-6">\n' +
        '                                <label  class=" am-form-label phone" ></label>\n' +
        '                            </div>\n' +
        '                            </div>\n' +
        '                            <button type="button" data-am-modal="{target:\'#huiban\'}" class="am-btn am-radius am-btn-secondary am-center" style="">添加会办单位</button>\n' +
        '                        </div>\n' +
        '                    </div>';
    $("#gaid").find("div").eq(0).after(box);

});
// 会办
let join_handler =[];
$("#go4").click(function () {
    if($("#userList1 span").hasClass("spanActive")){
        let dad1 = $(".spanActive").attr("value");
        $.ajax({
            url: testurl + "motion/handler_info",
            type: "POST",
            headers: { "Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "user_id":dad1//
            }),
            success:function (data) {

                $("#" + bbb + " "+".zhuban").html(data.obj.organ_name);
                $("#" + bbb + " "+".danwei").html(data.obj.dwxt);
                $("#" + bbb + " "+".contact").html(data.obj.realname);
                $("#" + bbb + " "+".phone").html(data.obj.phone);

                bbb++;
            }
        });
        join_handler.push(dad1);
        $('#huiban').modal('close');
        $("#userList1 span").removeClass("spanActive");
    }
    else {
        alert("请选择部门！")
    }

});